package orders

import (
	"gin_demo/internal/consts"
	"gin_demo/internal/model"
	"gin_demo/utility"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func AddOrder(ctx *gin.Context, req AddOrderReq) {
	order := model.Order{
		Number:           req.Number,
		UserName:         req.UserName,
		PayType:          req.PayType,
		Remark:           req.Remark,
		PayAt:            req.PayAt,
		Status:           req.Status,
		Price:            req.Price * 100,
		CouponPrice:      req.CouponPrice * 100,
		ActualPrice:      req.ActualPrice * 100,
		ConsigneeName:    req.ConsigneeName,
		ConsigneeAddress: req.ConsigneeAddress,
		ConsigneePhone:   req.ConsigneePhone,
		ShopImage:        req.ShopImage, // 商品图片
		ShopName:         req.ShopName,  // 商品名称
		ShopPrice:        req.ShopPrice, // 商品价格
		ShopNumber:       req.ShopNumber,
		OrderClass:       req.OrderClass, // 订单类型（三级分类，可选一级二级）

	}
	tx := consts.App.DB.Create(&order)
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, tx.Error.Error())
	} else {
		utility.Success(ctx, order)
	}
}

func List(ctx *gin.Context, req OrderRes) {
	var Count int64
	list := []ListOrderRes{}
	tmp := ListOrderRes{}
	order := []model.Order{}
	tx := consts.App.DB.Limit(req.Limit).Offset(req.Page - 1).Find(&order).Count(&Count)
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, tx.Error.Error())
	} else {
		for _, v := range order {
			tmp.Count = Count
			tmp.Number = v.Number
			tmp.ShopImage = v.ShopImage
			tmp.ShopName = v.ShopName
			tmp.ActualPrice = v.ActualPrice
			tmp.PayAt = v.PayAt
			tmp.PayType = v.PayType
			tmp.Status = v.Status
			tmp.UserName = v.UserName
			tmp.Remark = v.Remark
			tmp.OrderClass = v.OrderClass
			list = append(list, tmp)
		}
		utility.Success(ctx, list)
	}
}

func Delete(ctx *gin.Context, req SoftDeleteReq) {
	order := model.Order{}
	tx := consts.App.DB.Model(&order).Where("id=?", req.Id).Update("deleted_at", time.Time{})
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, "删除失败")
	} else {
		if tx.RowsAffected == 0 {
			consts.App.Log.Error("订单数据不匹配")

			utility.Fail(ctx, "删除失败,订单数据不匹配")

		} else {
			utility.Success(ctx, "删除成功")
		}

	}
}

func Update(ctx *gin.Context, req UpdateOrderReq) {
	order := model.Order{
		UserName:         req.UserName,
		PayType:          req.PayType,
		Remark:           req.Remark,
		PayAt:            req.PayAt,
		Status:           req.Status,
		Price:            req.Price * 100,
		CouponPrice:      req.CouponPrice * 100,
		ActualPrice:      req.ActualPrice * 100,
		ConsigneeName:    req.ConsigneeName,
		ConsigneeAddress: req.ConsigneeAddress,
		ConsigneePhone:   req.ConsigneePhone,
		ShopImage:        req.ShopImage, // 商品图片
		ShopName:         req.ShopName,  // 商品名称
		ShopPrice:        req.ShopPrice, // 商品价格
		ShopNumber:       req.ShopNumber,
		OrderClass:       req.OrderClass, // 订单类型（三级分类，可选一级二级）
	}
	tx := consts.App.DB.Model(&order).Where("id=? and number=?", req.Id, req.Number).Updates(&order)
	if tx.Error != nil {
		consts.App.Log.Error(tx.Error.Error())
		utility.Fail(ctx, "订单数据更新失败")
	} else {
		if tx.RowsAffected == 0 {
			consts.App.Log.Error("订单数据不匹配")

			utility.Fail(ctx, "删除失败,订单数据不匹配")

		} else {
			utility.Success(ctx, order)
		}

	}
}

func Search(ctx *gin.Context, req SearchOrder) {
	order := []model.Order{}
	switch req.Type {
	case "number":
		tx := consts.App.DB.Limit(req.Limit).Offset(req.Page-1).Where("number like ?", "%"+req.Number+"%").Find(&order)
		if tx.Error != nil {
			consts.App.Log.Error(tx.Error.Error())
			utility.Fail(ctx, tx.Error.Error())
		} else {
			if tx.RowsAffected == 0 {
				utility.Fail(ctx, "未查询到该订单信息")
			} else {
				utility.Success(ctx, order)
			}
		}
	case "order_class":
		tx := consts.App.DB.Limit(req.Limit).Offset(req.Page-1).Where("order_class like ?", "%"+req.OrderClass+"%").Find(&order)
		if tx.Error != nil {
			consts.App.Log.Error(tx.Error.Error())
			utility.Fail(ctx, tx.Error.Error())
		} else {
			if tx.RowsAffected == 0 {
				utility.Fail(ctx, "未查询到该订单信息")
			} else {
				utility.Success(ctx, order)
			}
		}
	case "status":
		tx := consts.App.DB.Limit(req.Limit).Offset(req.Page-1).Where("status like ?", "%"+strconv.Itoa(req.Status)+"%").Find(&order)
		if tx.Error != nil {
			consts.App.Log.Error(tx.Error.Error())
			utility.Fail(ctx, tx.Error.Error())
		} else {
			if tx.RowsAffected == 0 {
				utility.Fail(ctx, "未查询到该订单信息")
			} else {
				utility.Success(ctx, order)
			}
		}
	case "pay_type":
		tx := consts.App.DB.Limit(req.Limit).Offset(req.Page-1).Where("pay_type like ?", "%"+strconv.Itoa(req.PayType)+"%").Find(&order)
		if tx.Error != nil {
			consts.App.Log.Error(tx.Error.Error())
			utility.Fail(ctx, tx.Error.Error())
		} else {
			if tx.RowsAffected == 0 {
				utility.Fail(ctx, "未查询到该订单信息")
			} else {
				utility.Success(ctx, order)
			}
		}
	default:
		utility.Fail(ctx, "参数错误")
	}
}

// func search(req SearchOrder, order []model.Order) {
// 	tx := consts.App.DB.Limit(req.Limit).Offset(req.Page-1).Where(req.Type+" like ?", "%"+req.Number+"%").Find(&order)
// 	if tx.Error != nil {
// 		consts.App.Log.Error(tx.Error.Error())
// 		// utility.Fail(ctx, tx.Error.Error())
// 	} else {
// 		if tx.RowsAffected == 0 {
// 			// utility.Fail(ctx, "未查询到该订单信息")
// 		} else {
// 			// utility.Success(ctx, order)
// 		}
// 	}
// }
